/**
 * 
 */
package dms.presentation.action.student;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import dms.dto.LowElectricityDTO;
import dms.model.Dormitory;
import dms.model.form.LowElectricity;
import dms.model.util.PageIterator;
import dms.service.LowElectricityQueryService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.DateFormatUtil;
import dms.util.PageUtil;

/**
 * @author 李健
 *
 */
public class LowElectricityAction extends StudentBaseAction {
	
	private static final Log log = LogFactory.getLog(LowElectricityAction.class);
	
	public ActionForward lowElectricityList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LowElectricityDTO lowElectricityDTO = (LowElectricityDTO) form;
		String fromDateStr = lowElectricityDTO.getFromDate();
		String toDateStr = lowElectricityDTO.getToDate();
		Date fromDate = null;
		Date toDate = null;
		String type = null;
		int start;
		int count;
		Dormitory dormitory = this.getStudentWrapper().getStudent().getDormitory();
		LowElectricityQueryService lowElectricityQueryService = (LowElectricityQueryService) ContextUtil.getContext().getBean("lowElectricityQueryService");
		PageIterator<LowElectricity> lowElectricities = null;
		
		try {
			fromDate = DateFormatUtil.parseDate(fromDateStr);
			toDate = DateFormatUtil.parseDate(toDateStr);
		} catch (Exception e) {
			log.error("日期解释错误");
		}
		
		try {
			int[] params = PageUtil.parsePageParameter(request);
			start = params[0];
			count = params[1];
			type = lowElectricityDTO.getType();
			
			if (type != null && type.length() != 0) {
				if (type.equalsIgnoreCase("dormitory")) {
					lowElectricities = lowElectricityQueryService.findByDormitory(start, count, dormitory, fromDate, toDate);
				} 
			} else {
				lowElectricities = lowElectricityQueryService.findByDormitory(start, count, dormitory, fromDate, toDate);
			}
			request.setAttribute("lowElectricities", lowElectricities);
			request.setAttribute("student", this.getStudentWrapper().getStudent());
		} catch (Exception e) {
			return mapping.findForward("error");
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
}
